const assert = require("assert");

function getAnimals(fetch, id) {
  return fetch("https://reqres.in/api/users/" + id)
    .then((response) => response.json())
    .then((v) => {
      //   console.log(v)
      return v.results[0];
    });
}

describe("getAnimals", () => {
  it("calls fetch with the correct url", () => {
    const fakeFetch = (url) => {
      assert(url === "https://reqres.in/api/users/1");
      return new Promise((res) => {});
    };
    getAnimals(fakeFetch, 1);
  });

  it("parses the response of fetch correctly", (done) => {
    const fakeFetch = (url) => {
      return Promise.resolve({
        json: () =>
          Promise.resolve({
            results: [
              {
                name: "fluffy",
                type: "robot",
              },
            ],
          }),
      });
    };

    getAnimals(fakeFetch, 1).then((res) => {
      assert(res.name === "fluffy");
      done();
    });
  });
});
